#!/usr/bin/perl -w

###############################################################################
# Load the processed anno file into the anno table in the database
#
# Parameters needed: dbname, username, temp_dir
# Assumptions: $TEMP_DIR/anno.out exists
#
# Author: Herodotos Herodotou and Nedyalko Borisov
# Date: March 11, 2010
#
##############################################################################

use getOpts qw(get_options println);

if ($#ARGV != 0)
{
   println qq(Please specify the .properties file);
   println qq(Usage: perl $0 load.properties);
   exit(-1);
}

# Get input parameters
%opt = get_options($ARGV[0]);
$DB_NAME  =$opt{'dbname'};
$USERNAME =$opt{'username'};
$TEMP_DIR = $opt{'temp_dir'};

# Drop constraints, load the data, and add constraints
println qq(Loading anno data);
!system qq(psql -U $USERNAME -d $DB_NAME -f sql/drop_anno_constraints.sql > /dev/null) or die $!;
!system qq(psql -U $USERNAME -d $DB_NAME -c ).
        qq("\\COPY anno(manuf_id, platform, rsid, chromosome, location, alleleA, alleleB, reverse) ).
        qq(FROM '$TEMP_DIR/anno.out' WITH DELIMITER AS ','") or die $!;
!system qq(psql -U $USERNAME -d $DB_NAME -f sql/add_anno_constraints.sql > /dev/null) or die $!;
!system qq(psql -U $USERNAME -d $DB_NAME -c "ANALYZE anno" > /dev/null) or die $!;

# Remove the out files
!system qq(rm -f $TEMP_DIR/anno.out) or die $!;

println qq(Completed loading anno data);

